import Feeds
import Display
import Tkinter
import os
import _systemscheck

def loadURLs(FeedList):
        initialFeedURLs = {}
	initialFeedURLs['NYT'] = 'http://feeds.nytimes.com/nyt/rss/HomePage'
	initialFeedURLs['NYP'] = 'http://www.nypost.com/rss/news.xml'
	initialFeedURLs['LAT'] = 'http://feeds.latimes.com/latimes/news/nationworld/nation'
	initialFeedURLs['BAG'] = 'http://feeds.boston.com/boston/news/nation'
	initialFeedURLs['WSJ'] = 'http://online.wsj.com/xml/rss/3_8068.xml'

	for x in initialFeedURLs.keys():
		print 'LOADING: ' + x + '\n...'
		FeedList += [Feeds.URLfeed(initialFeedURLs[x])]
		print 'FINISHED\n\n'
	
	Feeds.create(FeedList)

def runsystem(JanusFeeds):
        Janus = Display.Reader(JanusFeeds)
        Janus.window.mainloop()

def _check(window,ans,confirm):
        if ans.get().lower() == 'y' or\
           ans.get().lower() == 'yes':
                window.destroy()
                JanusFeeds = Feeds.load()
                runsystem(JanusFeeds)
        elif ans.get().lower() == 'n' or\
             ans.get().lower() == 'no':
                window.destroy()
                JanusFeeds = []
                loadURLs(JanusFeeds)
                runsystem(JanusFeeds)
        else:
                ans.select_range(0,len(ans.get()))
                ans.select_clear()

if _systemscheck.run():
        window = Tkinter.Tk()
        Tkinter.Label(window,
                      text='Would you like to use \nprevious article build? (y/n)')\
                      .pack(side='left')
        ans = Tkinter.Entry(window,width=5)
        ans.pack(side='left')
        confirm = Tkinter.Button(window,text='OK',command=lambda: _check(window,ans,confirm))
        confirm.pack(side='left')
        window.mainloop()
else:
        JanusFeeds = []
        loadURLs(JanusFeeds)
        runsystem(JanusFeeds)
